<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>libdill</title>
  <link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>

<h1>libdill: Structured Concurrency for C</h1>

<ul id='toplist'>
<li><a href="index.html">Home</a></li>
<li><a href="download.html">Download</a></li>
<li><a href="documentation.html">Documentation</a></li>
<li><a href="tutorial.html">Tutorials</a></li>
<li><a href="faq.html">FAQ</a></li>
</ul>
<h1 id="name">NAME</h1>
<p>chrecv - receives a message from a channel</p>
<h1 id="synopsis">SYNOPSIS</h1>
<div class="sourceCode"><pre class="sourceCode c"><code class="sourceCode c"><span class="pp">#include </span><span class="im">&lt;libdill.h&gt;</span>

<span class="dt">int</span> chrecv(
    <span class="dt">int</span> ch,
    <span class="dt">void</span>* val,
    <span class="dt">size_t</span> len,
    <span class="dt">int64_t</span> deadline);</code></pre></div>
<h1 id="description">DESCRIPTION</h1>
<p>Receives a message from a channel.</p>
<p>The size of the message requested from the channel must match the size of the message sent to the channel. Otherwise, both peers fail with <strong>EMSGSIZE</strong> error.</p>
<p>If there's no one sending a message at the moment, the function blocks until someone shows up or until the deadline expires.</p>
<p><strong>ch</strong>: The channel.</p>
<p><strong>val</strong>: The buffer to receive the message to.</p>
<p><strong>len</strong>: Size of the buffer, in bytes.</p>
<p><strong>deadline</strong>: A point in time when the operation should time out, in milliseconds. Use the <strong>now</strong> function to get your current point in time. 0 means immediate timeout, i.e., perform the operation if possible or return without blocking if not. -1 means no deadline, i.e., the call will block forever if the operation cannot be performed.</p>
<h1 id="return-value">RETURN VALUE</h1>
<p>In case of success the function returns 0. In case of error it returns -1 and sets <strong>errno</strong> to one of the values below.</p>
<h1 id="errors">ERRORS</h1>
<ul>
<li><strong>EBADF</strong>: Invalid handle.</li>
<li><strong>ECANCELED</strong>: Current coroutine was canceled.</li>
<li><strong>EINVAL</strong>: Invalid argument.</li>
<li><strong>EMSGSIZE</strong>: The peer sent a message of different size.</li>
<li><strong>ENOTSUP</strong>: The handle does not support this operation.</li>
<li><strong>EPIPE</strong>: Channel has been closed with chdone.</li>
<li><strong>ETIMEDOUT</strong>: Deadline was reached.</li>
</ul>
<h1 id="example">EXAMPLE</h1>
<div class="sourceCode"><pre class="sourceCode c"><code class="sourceCode c"><span class="dt">int</span> val;
<span class="dt">int</span> rc = chrecv(ch, &amp;val, <span class="kw">sizeof</span>(val), now() + <span class="dv">1000</span>);
<span class="cf">if</span>(rc != <span class="dv">0</span>) {
    perror(<span class="st">&quot;Cannot receive message&quot;</span>);
    exit(<span class="dv">1</span>);
}
printf(<span class="st">&quot;Value %d received.</span><span class="sc">\n</span><span class="st">&quot;</span>, val);</code></pre></div>
<h1 id="see-also">SEE ALSO</h1>
<p><strong>chdone</strong>(3) <strong>chmake</strong>(3) <strong>chmake_mem</strong>(3) <strong>choose</strong>(3) <strong>chsend</strong>(3) <strong>now</strong>(3)</p>
</body>
